package org.google.maps;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

import org.google.json.JSONElement;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;

public class GoogleMapsActivity extends MapActivity {
	private MapView mapView;
	private MapController mapController;
	private GeoPoint mapCenterGeoPoint;

	double fromLat = 12.303534;
	double fromLong = 76.64611;
	double toLat = 12.9715987;
	double toLong = 77.5945627;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		mapView = (MapView) findViewById(R.id.map_view);
		mapController = mapView.getController();

		// mapView.setSatellite(true);
		mapView.setBuiltInZoomControls(true);

		mapView.setOnTouchListener(new View.OnTouchListener() {

			public boolean onTouch(View arg0, MotionEvent arg1) {
				// TODO Auto-generated method stub
				// String s = "lll";
				return false;
			}
		});

		WebRequest request = new WebRequest();
		JSONObject object = request.receive();

		JSONElement el = new JSONElement();
		el.parse(object);
		float[] coordinate = el.getPoints();

		List<GeoPoint> points = new ArrayList<GeoPoint>();
		GeoPoint point;
		for (int i = 0; i < coordinate.length - 2; i = i + 2) {
			point = new GeoPoint((int)(coordinate[i] * 1E6), (int)(coordinate[i+1] * 1E6));
			points.add(point);
		}
		 Polyline pl = new Polyline(points); pl.SetColor(Color.CYAN);
		 mapView.getOverlays().add(pl);
		 //mapController.animateTo(p1);
		 
		// GeoPoint p1 = new GeoPoint(1, 1);
		// GeoPoint p2 = new GeoPoint(12, 10);

		/*
		 * GeoPoint p1 = new GeoPoint((int)(45 * 1E6), (int)(25 * 1E6));
		 * GeoPoint p2 = new GeoPoint((int)(45.123 * 1E6), (int)(25 * 1E6));
		 * GeoPoint p3 = new GeoPoint((int)(45 * 1E6), (int)(24.123 * 1E6));
		 * GeoPoint p4 = new GeoPoint((int)(46.342 * 1E6), (int)(26.4234 *
		 * 1E6));
		 * 
		 * List<GeoPoint> points = new ArrayList<GeoPoint>(); points.add(p1);
		 * points.add(p2); points.add(p3); points.add(p4);
		 * 
		 * Polyline pl = new Polyline(points); pl.SetColor(Color.CYAN);
		 * mapView.getOverlays().add(pl);
		 * 
		 * mapController.animateTo(p1);
		 */

		// mapController.setZoom(14);

	}

	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}
}